<?php
	
// Loader
include_once "Zend/Controller/Front.php";
//Zend_Loader::registerAutoload();

// Controller
$front = Zend_Controller_Front::getInstance();

// Configuration
include_once 'Zend/Config/Ini.php';
$cfg = new Zend_Config_Ini(ROOT . '/config/config.ini', APPLICATION);

date_default_timezone_set((isset($_COOKIE['TZ']))?$_COOKIE['TZ']:$cfg->timezone);

if(isset($cfg->debug)){
	if(isset($logger)) $logger->debug('Debug Turned On');
	error_reporting(E_ALL|E_STRICT);
	ini_set('display_errors', 1);
	$front->throwExceptions(true);
	include_once "Tamer/Debug.php";
}

if($cfg->logger){
	// Zend_Log
	include_once "Zend/Log.php";
	include_once "Zend/Log/Writer/Stream.php";
	$writer = new Zend_Log_Writer_Stream(ROOT . '/log/' . APPLICATION . '.txt');
	$logger = new Zend_Log($writer);
	if($logger){
		$logger->info('Zend Log Start in bootstrap');
	}
}

include_once "Zend/Registry.php";
$registry = new Zend_Registry(array(), ArrayObject::ARRAY_AS_PROPS);
Zend_Registry::setInstance($registry);
$registry->set('config', $cfg);

//echo '<pre>';print_r($registry);

// Database
include_once "Zend/Db/Table.php";
$dbMySQL = Zend_Db::factory($cfg->database->adapter, 
							$cfg->database->params->toArray());
Zend_Db_Table::setDefaultAdapter($dbMySQL);
Zend_Registry::set('mysql', $dbMySQL);

// Sessions Start
include_once "Zend/Session.php";
$cfgses = new Zend_Config_Ini(ROOT . '/config/config.ini', 'session');
Zend_Session::setOptions($cfgses->toArray());
Zend_Session::start();

// Create auth object
include_once "Zend/Auth.php";
include_once "Tamer/Plugin/Auth.php";
$auth = Zend_Auth::getInstance();
if ($auth->hasIdentity()) {
	$alias = $auth->getIdentity()->alias;
	$role = $auth->getIdentity()->role;
}else{
	$alias = '';
	$role = 'guest';
}

// Create acl object
include_once "Tamer/Acl.php";
$acl = new Tamer_Acl($alias, $role);
echo '<pre>';print_r($acl);echo '</pre>';exit;

// Layout
include_once "Zend/Layout.php";
Zend_Layout::startMvc(array('layoutPath'=>ROOT . '/layout'));

// Controller
$front->setControllerDirectory(APPS . '/default/controllers')
				->setBaseUrl($cfg->baseUrl)
				->registerPlugin(new Tamer_Plugin_Auth($auth, $acl))
				->returnResponse(true);
$front->addControllerDirectory(APPS . '/default/modules/safe/controllers', 'safe');

$response = $front->dispatch();
if ($response->isException()) {
    $exceptions = $response->getException();
    echo '<pre>Exception: '. $exceptions->getMessage().'</pre>';
} else {
    $response->sendHeaders();
    $response->outputBody();
}

?>